package com.estate.common.utils;

import com.estate.common.core.domain.model.Pagination;
import lombok.Data;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Class description
 * @param <T>
 * @author     Simon
 * CreateTime  2019-10-21  18:29:34
 * Description 分页查询输出给前端的业务数据
 * @param <T>  泛型
 */
@Data
public class PageOutPutBean<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    @Valid
    private Pagination pagination;

    /** 业务数据 */
    private List<T> data;

    /**
     * Method description
     * @author:Simon
     * CreateTime:2019-10-21  18:29:14
     * Description:
     *
     * @return 返回业务数据
     */
    public List<T> getData() {
        return data;
    }

    /**
     * Method description
     * @author:崔正旺
     * CreateTime:2019-10-21  18:29:14
     * Description:
     *
     * @param data 业务数据
     */
    public void setData(List<T> data) {
        this.data = data;
    }


    public PageOutPutBean<T> getPageOutPutBeanList(
        int pageNo, int pageSize, List<T> specialFeatureResps) {
        //总数
        int total = specialFeatureResps.size();
        //总页数
        int pageSum = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        //分页
        List<T> pageList = specialFeatureResps.stream()
            .skip((pageNo - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
        setData(pageList);
        setPagination(new Pagination(pageNo, pageSize, pageSum, total));
        return self();
    }


    @SuppressWarnings("unchecked")
    protected PageOutPutBean<T> self() {
        return this;
    }
}
